package com.sunny.excel.excel.uploader.impl;



import com.sunny.excel.excel.uploader.ExcelUploadManager;
import com.sunny.excel.excel.uploader.ExcelUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 开启excel扫描才 加入容器
 */

@Component
public class ExcelUploadManagerImpl implements ExcelUploadManager {
    private List<ExcelUploader> processors;

    @Autowired
    public void setProcessors(List<ExcelUploader> processors) {
        this.processors = processors;
        Collections.sort(processors, Comparator.comparing(ExcelUploader::getOrder));
    }

    @Override
    public <T> List<T> process(String fileExt, InputStream inputStream, int sheetIndex, Class<T> dataClass,
        String... contextParam) throws IOException {

        for (ExcelUploader processor : processors) {
            if (processor.canProcess(dataClass)) {
                return processor.process(inputStream, sheetIndex, dataClass, fileExt, contextParam);
            }
        }
        throw new RuntimeException("文件上传错误：请上传后缀为.xls或者.xlsx的文件！");
    }
}
